﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGKata
{
    class Character
    {
        private string name;
        private int strength;
        private int dexterity;
        private int tempDexterity;
        private int constitution;
        private int rollDexterity;
        private bool die = false;

        private Weapon weapon;
        private Armor armor;
        private Character opponent;

        public Character()
        {
            
        }

        public void setName(string n)
        {
            name = n;
        }

        public string getName()
        {
            return name;
        }

        public void setStrength(string num)
        {
            while (!Roll.isValidInt(num))
            {
                Console.WriteLine("That's not a number! Please choose again.");
                num = Console.ReadLine();
            }
            int number = int.Parse(num);
            strength = number;
        }

        public int getStrength()
        {
            return strength;
        }
        public void setDexterity(string num)
        {
            while (!Roll.isValidInt(num))
            {
                Console.WriteLine("That's not a number! Please choose again.");
                num = Console.ReadLine();
            }
            int number = int.Parse(num);
            dexterity = number;
        }

        public void changeDexterity()
        {
            if (armor.getEncumbrance() > 0)
            {
                tempDexterity = dexterity - armor.getEncumbrance();
            }
        }

        public int getTempDexterity()
        {
            return tempDexterity;
        }

        public void resetTempDexterity()
        {
            tempDexterity = dexterity;
        }

        public int getDexterity()
        {
            return dexterity;
        }

        public void setConstitution(string num)
        {
            while (!Roll.isValidInt(num))
            {
                Console.WriteLine("That's not a number! Please choose again.");
                num = Console.ReadLine();
            }
            int number = int.Parse(num);
            constitution = number;
        }

        public int getConstitution()
        {
            return constitution;
        }

        public bool hasOpponent()
        {
            return opponent != null;
        }

        public bool isWeaponEquipped()
        {
            return weapon != null;
        }
        public bool isArmorEquipped()
        {
            return armor != null;
        }

        public void setOpponent(Character c)
        {
            opponent = c;
        }

        public Character getOpponent()
        {
            return opponent;
        }
        public void setWeapon(Weapon w)
        {
            weapon = w;
        }

        public Weapon getWeapon()
        {
            return weapon;
        }
        public void setArmor(Armor a)
        {
            armor= a;
            resetTempDexterity();
            changeDexterity();
        }

        public Armor getArmor()
        {
            return armor;
        }

        public void Attack()
        {
            if(Roll.rolls(opponent.tempDexterity) - Roll.rolls(this.tempDexterity) > 0)
            {
                int value = attackDamage() - opponent.getArmor().getProtection();
                if (value > 0)
                {
                    //  put some writelines in here.
                    opponent.takeDamage(value);
                }
            }
        }

        public int attackDamage()
        {
            if (isWeaponEquipped())
            {
                return Roll.rolls(this.weapon.getDamage()) + Roll.rolls(getBaseAttribute());
            }
            else
            {
                return Roll.rolls(0) + Roll.rolls(strength);
            }
        }

        public void setRollDexterity()
        {
            rollDexterity = Roll.rolls(0, tempDexterity); 
        }

        public int getRollDexterity()
        {
            return rollDexterity;
        }

        public int getBaseAttribute()
        {
            switch (this.weapon.getBaseAttribute())
            {
                case "strength":
                    return strength;
                case "dexterity":
                    return dexterity;
                default:
                    return 0;
            }            
        }
        public void takeDamage(int damage)
        {
            constitution = constitution - damage;
            if (constitution < 1)
            {
                die = true;
            }
        }

        public bool isDead()
        {
            return die;
        }       
    }
}
